package spio

import (

	// "gobot.io/x/gobot"
	// "gobot.io/x/gobot/drivers/gpio"
	// "gobot.io/x/gobot/drivers/i2c"
	// "gobot.io/x/gobot/drivers/spi"
	"fmt"
	"io/ioutil"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/drivers/gpio"
	"gobot.io/x/gobot/drivers/i2c"
	"gobot.io/x/gobot/drivers/spi"
	"gobot.io/x/gobot/platforms/raspi"
	"gobot.io/x/gobot/sysfs"
)

// make sure that this Adaptor fullfills all the required interfaces
var _ gobot.Adaptor = (*raspi.Adaptor)(nil)
var _ gpio.DigitalReader = (*raspi.Adaptor)(nil)
var _ gpio.DigitalWriter = (*raspi.Adaptor)(nil)
var _ gpio.PwmWriter = (*raspi.Adaptor)(nil)
var _ gpio.ServoWriter = (*raspi.Adaptor)(nil)
var _ sysfs.DigitalPinnerProvider = (*raspi.Adaptor)(nil)
var _ sysfs.PWMPinnerProvider = (*raspi.Adaptor)(nil)
var _ i2c.Connector = (*raspi.Adaptor)(nil)
var _ spi.Connector = (*raspi.Adaptor)(nil)

var readFile = func() ([]byte, error) {
	return ioutil.ReadFile("/proc/cpuinfo")
}

func initTestAdaptor() *raspi.Adaptor {
	readFile = func() ([]byte, error) {
		return []byte(`
Hardware        : BCM2708
Revision        : 0010
Serial          : 000000003bc748ea
`), nil
	}
	a := raspi.NewAdaptor()
	a.Connect()
	return a
}

func UserAdaptorDigitalIO() {
	a := initTestAdaptor()
	fs := sysfs.NewMockFilesystem([]string{
		"/sys/class/gpio/export",
		"/sys/class/gpio/unexport",
		"/sys/class/gpio/gpio4/value",
		"/sys/class/gpio/gpio4/direction",
		"/sys/class/gpio/gpio27/value",
		"/sys/class/gpio/gpio27/direction",
	})

	sysfs.SetFilesystem(fs)

	// a.DigitalWrite("19", 1)

	time.Sleep(time.Second * 1)
	//i, _ := a.DigitalRead("21")
	//fmt.Print(i, "\r\n")

	//time.Sleep(time.Second * 2)

	a.DigitalWrite("21", 1)
	i, _ := a.DigitalRead("21")
	fmt.Print(i, "\r\n")

	// fs.WithReadError = true
	// _, err := a.DigitalRead("13")
	// if err != nil {
	// 	fmt.Println(err)
	// }

	// fs.WithWriteError = true
	// _, err = a.DigitalRead("7")

	// if err != nil {
	// 	fmt.Println(err)
	// }

}
